package com.likuicat.picture_ddd.interfaces.controller;

import com.likuicat.picture_ddd.infrastructure.common.BaseResponse;
import com.likuicat.picture_ddd.infrastructure.common.ResultUtils;
import com.likuicat.picture_ddd.infrastructure.exception.ErrorCode;
import com.likuicat.picture_ddd.infrastructure.exception.ThrowUtils;
import com.likuicat.picture_ddd.interfaces.dto.space.analyze.*;
import com.likuicat.picture_ddd.domain.space.entity.Space;
import com.likuicat.picture_ddd.domain.user.entity.User;
import com.likuicat.picture_ddd.interfaces.vo.space.analyze.*;
import com.likuicat.picture_ddd.application.service.SpaceAnalyzeApplicationService;
import com.likuicat.picture_ddd.application.service.UserApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "空间分析管理接口")
@RestController
@RequestMapping("/space/analyze")
@Slf4j
public class SpaceAnalyzeController {

    @Resource
    private UserApplicationService userApplicationService;

    @Resource
    private SpaceAnalyzeApplicationService spaceAnalyzeApplicationService;

    @ApiOperation("获取空间资源使用分析数据")
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,
                                                                        HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUsageAnalyzeRequest == null,ErrorCode.NOT_FOUND_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        SpaceUsageAnalyzeResponse spaceUsageAnalyze = spaceAnalyzeApplicationService.getSpaceUsageAnalyze(spaceUsageAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUsageAnalyze);
    }

    @ApiOperation("获取空间图片分类分析数据")
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyzeList(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest,
                                                                                        HttpServletRequest request){
        ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null,ErrorCode.NOT_FOUND_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyzeList = spaceAnalyzeApplicationService.getSpaceCategoryAnalyzeList(spaceCategoryAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceCategoryAnalyzeList);
    }

    @ApiOperation("获取空间图片标签分析数据")
    @PostMapping("/tag")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagsAnalyzeList(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest,
                                                                               HttpServletRequest request){
        ThrowUtils.throwIf(spaceTagAnalyzeRequest == null,ErrorCode.NOT_FOUND_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> spaceTagAnalyzeList = spaceAnalyzeApplicationService.getSpaceTagAnalyzeList(spaceTagAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyzeList);
    }

    @ApiOperation("获取空间图片大小分析数据")
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyzeList(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest,
                                                                                HttpServletRequest request){
        ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null,ErrorCode.NOT_FOUND_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponse> spaceSizeAnalyzeList = spaceAnalyzeApplicationService.getSpaceSizeAnalyzeList(spaceSizeAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceSizeAnalyzeList);
    }

    @ApiOperation("获取用户上传行为分析数据")
    @PostMapping("/user")
    public BaseResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyzeList(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest,
                                                                                HttpServletRequest request){
        ThrowUtils.throwIf(spaceUserAnalyzeRequest == null,ErrorCode.NOT_FOUND_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> spaceUserAnalyzeList = spaceAnalyzeApplicationService.getSpaceUserAnalyzeList(spaceUserAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUserAnalyzeList);
    }

    @ApiOperation("获取空间使用排行分析数据")
    @PostMapping("/rank")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userApplicationService.getLoginUser(request);
        List<Space> resultList = spaceAnalyzeApplicationService.getSpaceRankAnalyzeList(spaceRankAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }
}
